package com.starleague.common;

import java.util.Collection;
import java.util.Map;

public interface HasInfoProperties
{
    Map<String, Object> infoProperties();

    String infoPropertyClassName();

    static class Dumper
    {
        protected static final String INDENT = "    ";

        public static String toString(HasInfoProperties hasInfoProperties,
                                      int depth)
        {
            return "\n\n"
                   + INDENT
                   + toString("\n" + INDENT + INDENT, hasInfoProperties, depth)
                   + "\n";
        }

        public static String toString(String indentStr,
                                      HasInfoProperties hasInfoProperties,
                                      int depth)
        {
            StringBuilder sb = new StringBuilder(hasInfoProperties.infoPropertyClassName());
            for (Map.Entry<String, Object> infoProperty : hasInfoProperties.infoProperties().entrySet())
            {
                Object value = infoProperty.getValue();
                if (value instanceof HasInfoProperties)
                {
                    if (depth >= 0)
                        sb.append(indentStr
                                  + infoProperty.getKey()
                                  + " = "
                                  + toString(indentStr + INDENT,
                                             (HasInfoProperties) value,
                                             depth - 1));
                }
                else if (value instanceof Collection<?>)
                {
                    Collection<?> subValueCollection = (Collection<?>) value;
                    sb.append(indentStr
                              + infoProperty.getKey()
                              + " = ["
                              + subValueCollection.size()
                              + "]");

                    if (depth > 0)
                        for (Object subValue : subValueCollection)
                            if (subValue instanceof HasInfoProperties)
                            {
                                sb.append(indentStr
                                          + INDENT
                                          + toString(indentStr + INDENT + INDENT,
                                                     (HasInfoProperties) subValue,
                                                     depth - 1));
                            }
                            else
                            {
                                sb.append(indentStr + INDENT + subValue);
                            }
                }
                else
                {
                    sb.append(indentStr + infoProperty.getKey() + " = " + value);
                }
            }
            return sb.toString();
        }
    }
}
